<script>
	import $http from 'utils/api.js';
	import $api from 'utils/url.js';

	export default {
		onLaunch: function() {
			console.log('App Launch')
			this.getUpdateManager();
		},
		onShow: function(e) {
			console.log(e.path)
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 文字弹窗
			getMsg(e, s) {
				uni.showToast({
					title: e,
					icon: s ? s : 'none',
					duration: 2000
				});
			},
			// 跳转页面
			getNavigateTo(e) {
				uni.navigateTo({
					url: e
				});
			},
			// 返回上一级
			getBack() {
				setTimeout(() => {
					uni.navigateBack();
				}, 1500)
			},
			// 登录
			getUserLogin(parentOpenid, fun) {
				let self = this;
				uni.login({
					success(res) {
						if (res.code) {
							console.log('code:',res.code,'parentOpenid:',parentOpenid);
							$http.post($api.login, {
								code: res.code,
								parent_openid: parentOpenid || '',
							}).then(result => {
								uni.setStorageSync('openid', result.data);
								if (fun) fun(result);
							})
						} else {
							self.getMsg('登录失败：' + res.errMsg);
						}
					},
					fail(e) {
						console.log(e);
						self.getMsg('登录失败：' + e);
					}
				})
			},
			// 预览图片
			getBigImg(e, i, s) {
				let arr = [];

				if (s) {
					arr = e;
				} else {
					e.map((n) => {
						if (n.indexOf("http") == -1) {
							n = $api.yuming + n
						}
						arr.push(n)
					})
				}

				uni.previewImage({
					urls: arr,
					current: i,
					longPressActions: {
						itemList: ['保存图片'],
						success: function(data) {
							console.log(data);
							if (data.tapIndex == 0) {
								uni.saveImageToPhotosAlbum({
									filePath: arr[data.index],
									success: function() {
										console.log('save success');
									}
								});
							}
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			// 预览文档
			getSeeFile(e) {
				uni.showLoading();
				uni.downloadFile({
					url: e,
					success: function(res) {
						var filePath = res.tempFilePath;
						uni.openDocument({
							filePath: filePath,
							showMenu: true,
							success: function(res) {
								uni.hideLoading();
								console.log('打开文档成功');
							}
						});
					}
				});
			},
			// 自动更新
			getUpdateManager() {
				let self = this;
				if (uni.canIUse('getUpdateManager')) {
					// 当前版本可用
					const updateManager = uni.getUpdateManager();
					updateManager.onCheckForUpdate(res => {
						if (res.hasUpdate) {
							updateManager.onUpdateReady(function() {
								uni.showModal({
									title: '更新提示',
									content: '新版本已更新，需要重启应用',
									showCancel: false,
									success: ({
										confirm,
										cancel
									}) => {
										updateManager.applyUpdate()
									}
								})
							})
							updateManager.onUpdateFailed(function() {
								// 新的版本下载失败
								uni.showModal({
									title: '已经有新版本了哟~',
									content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
								})
							})
						}
					})
				} else {
					uni.showModal({
						title: "提示",
						content: "当前版本无法使用该功能，请更新微信版本！"
					});
				}
			},
      // 获取当前图片上传要编辑的对象
      getActiveImage() {
        return this.globalData.activeImage;
      },
      // 设置当前图片上传要编辑的对象
      setActiveImage(d = null) {
        this.globalData.activeImage = d;
      }
		},
    globalData: {
      activeImage: null,
    }
	}
</script>

<style>
	/*每个页面公共css */
	@import url("/static/css/public.css");

	image,
	input,
	navigator {
		display: block;
	}

	view {
		color: #222;
	}

	button {
		display: block;
		border: none !important;
	}
</style>